package com.yhyvr.console.controller.main;

import com.lz.kit.LogKit;
import com.lz.kit.Prop;
import com.lz.kit.PropKit;
import com.lz.tool.PathKit;
import com.lz.tool.ToolClassSearcher;
import com.lz.tool.UUIDTool;
import org.springframework.context.annotation.ClassPathBeanDefinitionScanner;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.sql.DataSource;
import java.lang.reflect.Method;
import java.util.Enumeration;
import java.util.List;

/**
 * Created by tech6 on 2016/9/19.
 */
public class Main {

    private static DataSource dataSource;
    private static JdbcTemplate jdbcTemplate;

    static {
        Prop prop = PropKit.use("three-app.properties");
        dataSource = getDataSource(prop);
        jdbcTemplate = new JdbcTemplate(dataSource);
    }

    private static DriverManagerDataSource getDataSource(Prop map) {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(map.get("jdbc.driver"));
        dataSource.setUrl(map.get("jdbc.url"));
        dataSource.setUsername(map.get("jdbc.username"));
        dataSource.setPassword(map.get("jdbc.password"));
        return dataSource;
    }
    public static void main(String[] args) {
            getListReq();
    }

    /**
     * 获取请求链接
     */
    public  static void getListReq() {
        List<Class<?>> modelClasses = new ToolClassSearcher(null).search(null);// 查询所有继承BaseModel的类
        // 循环处理自动注册映射
        for (Class model : modelClasses) {
            if(model.getAnnotation(Controller.class)!=null) {
                Method[] method = model.getMethods();
                for (Method method1 : method) {
                    RequestMapping r =  method1.getAnnotation(RequestMapping.class);
                    if(r!=null){
                        String [] s= r.value();
                        for (String s1 : s) {
                            if(s1.startsWith("/console")){
                                if(s1.startsWith("/console/login")){
                                    continue;
                                }
                                String title="";
                                Prop prop =  PropKit.use("table-description.properties");
                                Enumeration e =  prop.getProperties().propertyNames();
                                while (e.hasMoreElements()){
                                    String name = (String) e.nextElement();
                                    if(s1.indexOf(name)!=-1){
                                        title+=prop.get(name);
                                        break;
                                    }
                                }
                                if(s1.endsWith("/add")){
                                    title+="-添加页面";
                                } else if(s1.endsWith("/save")){
                                    title+="-保存操作";
                                }else if(s1.endsWith("/edit")){
                                    title+="-编辑页面";
                                }else if(s1.endsWith("/list")){
                                    title+="-列表页面";
                                }else if(s1.endsWith("/json")){
                                    title+="-列表json数据请求";
                                }
                                else if(s1.endsWith("/delete")){
                                    title+="-删除";
                                }
                              int c =   jdbcTemplate.queryForObject("select count(id) from sys_req_url where url=?",Integer.class,s1);
                                if(c==0)
                               jdbcTemplate.update("INSERT INTO sys_req_url(id,url,name,description)VALUES(?,?,?,?)", UUIDTool.getUUID(),s1,title,title);
                            }

                        }
                    }
                }
            }
        }
    }
}
